package com.JL;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class JLRSACoderTest {

    private static final  String privateKey="MTAwMzUyNTMwOTM4MTMwMTgzODc5MzkzNTcxMjU0MDYzMTQ1NDMwMTAwNjcyNTg0NzM4MDYyODYxMDIwOTQ1NzA3NzA2NzYyNjMyNDUxNjk5OTE4MTMyNTQ5NDg2NjQ0MDc1MzEzOTkxMjAyNTA1OTUxMjM3OTE5MzExNjAyMDIxNzI4NzkyNTI1NzY2NDkxOTk5NTgzMTg2ODk2MjcxNjk2NDc1OTcyOTc1MjY0Mzg4ODY1OTc3MjY0ODg5MzgwMTcwNDA1MjM4ODcyNzgwNDg4NDM2NjQ0MDkyMTEwOTY2ODI0NDAwMzQxMTA2NjkzNDM5MjUzNDMzMDU4MTg3MzM4MDA3NDQyMjQwNDg2MDcxNTQ0MzAwNTQ0MzQ3NTk1OTg2ODM0MTc0NzYwODc4MzUxOTgxLS0tLTE1NTcyNjU3MjcyMDg3Mjc4NDg0NzI1MTU3MDgxNTU0MjY5OTM5NDg2NDU1NTkyODIyMTYzMDQyMTk4ODA0MDAxNTE2MzYxMzgzMjE5MTU1NDEwOTQ5NjYyNDU0NDc4Njk1MTc4OTU2NzgwNTg5MzY5NzMxNDQzOTA4MDA2MDUwOTQ4MDQxODY5MjgwNjM4Nzc4NjQwNDc4MDU4NjM2NjA1OTk5NTY4MDM1MzU4NzcwNTc5OTUxMDk3ODE4ODg5MTQ4MjA5NjY2NzAzNzMyMzI1MDIwODA2ODc4Njg4ODc4OTk1Nzc5MzEwNTY4MTcyMjg5Njk2MjYyMTY2MjM1MDE1NzIxOTE1NjI2NTU0NDc2NjY1MzA5NTIzMDQ4NzI1MTQxODA3ODQyNzI1MDg5MTM1NjU=";
    private static final  String publicKey="MTAwMzUyNTMwOTM4MTMwMTgzODc5MzkzNTcxMjU0MDYzMTQ1NDMwMTAwNjcyNTg0NzM4MDYyODYxMDIwOTQ1NzA3NzA2NzYyNjMyNDUxNjk5OTE4MTMyNTQ5NDg2NjQ0MDc1MzEzOTkxMjAyNTA1OTUxMjM3OTE5MzExNjAyMDIxNzI4NzkyNTI1NzY2NDkxOTk5NTgzMTg2ODk2MjcxNjk2NDc1OTcyOTc1MjY0Mzg4ODY1OTc3MjY0ODg5MzgwMTcwNDA1MjM4ODcyNzgwNDg4NDM2NjQ0MDkyMTEwOTY2ODI0NDAwMzQxMTA2NjkzNDM5MjUzNDMzMDU4MTg3MzM4MDA3NDQyMjQwNDg2MDcxNTQ0MzAwNTQ0MzQ3NTk1OTg2ODM0MTc0NzYwODc4MzUxOTgxLS0tLTY1NTM3";

    /*生成licence文件
    * */
    public static void main(String[] args) throws Exception {

       // Map<String, Object> keyMap = JLRSACoder.initKeys("julingkeji");
        Map<String,String> datamap=new HashMap<>();
        String pubKeyBase64str=publicKey;
        String priKeyBase64str=privateKey;
        datamap.put("Product.name","承德市招生办中考系统");
        datamap.put("Product.version","2.0");
        datamap.put("License.type","年度用户");
        datamap.put("License.expiry","2021-10-01");
        String str="{\"Product.name\":\"承德市招生办中考系统\",\"Product.version\":\"2.0\",\"License.type\":\"年度用户\",\"License.expiry\":\"2021-10-01\"}";
        str= Base64.getEncoder().encodeToString(str.getBytes());
        byte[] b=Base64.getDecoder().decode(str);
        String pubKeystr= new String(Base64.getDecoder().decode(pubKeyBase64str));
        String priKeystr= new String(Base64.getDecoder().decode(priKeyBase64str));

        //N、e
        String pubKeystr_N=pubKeystr.substring(0,pubKeystr.indexOf("----"));
        String pubKeystr_E=pubKeystr.substring(pubKeystr.indexOf("----")+4,pubKeystr.length());

        //N、d
        String priKeystr_N=priKeystr.substring(0,priKeystr.indexOf("----"));
        String priKeystr_D=priKeystr.substring(priKeystr.indexOf("----")+4,priKeystr.length());

        PublicKey publicKey = JLRSACoder.getPublicKey(pubKeystr_N,pubKeystr_E);
        PrivateKey privateKey =JLRSACoder.getPrivateKey(priKeystr_N,priKeystr_D);




        byte[] encoderData = JLRSACoder.encryptRSA(str.getBytes(), privateKey);
        String sign = JLRSACoder.sign(encoderData, privateKey);
        boolean status = JLRSACoder.verify(encoderData, sign, publicKey);

        byte[] decoderData =JLRSACoder.decryptRSA(encoderData,publicKey);

        System.out.println("原文：" + str);

        System.out.println("密文：" +new String(Base64.getEncoder().encode(encoderData)));

        System.out.println("解密文：" + new String(decoderData));

        System.out.println("签名：" + sign);

        System.out.println("验证结果：" + status);



    }
}
